<?php

namespace backend\modules\home\models;

use Yii;

/**
 * This is the model class for table "lw_article".
 *
 * @property int $id
 * @property int $cid
 * @property int $type
 * @property string $title
 * @property string $summary
 * @property string $author
 * @property int $read
 * @property int $is_hot
 * @property int $is_recommend
 * @property string $tag
 * @property int $order
 * @property string $image_path
 * @property string $url
 * @property string $content
 * @property int $status
 * @property string $publish_date
 * @property string $create_time
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'lw_article';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cid', 'read', 'order', 'type', 'is_hot', 'is_recommend', 'status'], 'integer'],
            [['content'], 'string'],
            [['publish_date'], 'required'],
            [['title', 'tag', 'appendix'], 'string', 'max' => 128],
            [['summary', 'image_path', 'url'], 'string', 'max' => 255],
            [['author'], 'string', 'max' => 50],
            [['publish_date', 'create_time'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cid' => '文章类别',
            'type' => '文章类型',
            'title' => '文章标题',
            'summary' => '文章概要',
            'author' => '作者',
            'read' => '阅读量',
            'is_hot' => '是否热门',
            'is_recommend' => '是否推荐',
            'tag' => '关键词',
            'order' => '排序',
            'image_path' => '图片路径',
            'url' => '文章链接',
            'content' => '内容',
            'status' => '状态',
            'publish_date' => '发布日期',
            'create_time' => '创建时间',
            'appendix' => '附件',
        ];
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                $this->create_time = time();
                
                if(empty($this->order)){
                    $this->order = 0;
                }
                if(empty($this->read)){
                    $this->read = 0;
                }
                if(empty($this->cid)){
                    $this->cid = 0;
                }
                if(empty($this->is_hot)){
                    $this->is_hot = 0;
                }
                if(empty($this->is_recommend)){
                    $this->is_recommend = 0;
                }
                if(empty($this->type)){
                    $this->type = 0;
                }
            }
            return true;
        }
        return false;
    }
}
